'use strict';

/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const {router, controller, config, middleware} = app;

  const userRequired = middleware.userRequired();
  const adminRequired = middleware.adminRequired();
  const createTopicLimit = middleware.createTopicLimit(config.topic);
  const createUserLimit = middleware.createUserLimit(config.create_user_per_ip);

  // home page
  router.get('/', controller.siteController.index);
  router.get('/indexScroll', controller.siteController.indexScroll);
  router.get('/tag/:tag', controller.siteController.listByTags);
  router.get('/listByTagsScroll', controller.siteController.listByTagsScroll);

  // sitemap
  router.get('/sitemap.xml', controller.siteController.sitemap);
  // mobile app download
  router.get('/app/download', controller.siteController.appDownload);

  // sign controller
  if (config.allow_sign_up) {
    // 跳转到注册页面
    router.get('/signup', controller.signController.showSignup);
    // 提交注册信息
    router.post('/signup', createUserLimit, controller.signController.signup);
  } else {
    // 进行github验证
    // router.redirect('/signup', '/passport/github');
  }


  router.get('/signin', controller.signController.showLogin); // 进入登录页面
  router.post('/login', controller.signController.login); // 登录
  router.all('/signout', controller.signController.signout); // 登出
  router.get('/active_account', controller.signController.activeAccount); // 帐号激活

  router.get('/search_pass', controller.signController.showSearchPass); // 找回密码页面
  router.post('/search_pass', controller.signController.updateSearchPass); // 找回密码
  router.get('/reset_pass', controller.signController.resetPass); // 进入重置密码页面
  router.post('/reset_pass', controller.signController.updatePass); // 更新密码

  // user controller
  router.get('/user_square', controller.userController.alluser); // 所有用户页面
  router.get('/setting', userRequired, controller.userController.showSetting); // 用户个人设置页
  router.post('/setting', userRequired, controller.userController.setting); // 提交个人信息设置
  router.get('/stars', controller.userController.listStars); // 显示所有达人列表页
  router.get('/users/top100', controller.userController.top100); // 显示积分前一百用户页
  router.get('/user/:name/replies', controller.userController.listReplies); // 用户参与的所有回复页
  router.post('/user/set_star', adminRequired, controller.userController.toggleStar); // 把某用户设为达人
  router.post('/user/cancel_star', adminRequired, controller.userController.toggleStar); // 取消某用户的达人身份
  router.post('/user/:name/block', adminRequired, controller.userController.block); // 禁言某用户
  router.post('/user/:name/delete_all', adminRequired, controller.userController.deleteAll); // 删除某用户所有发言
  router.get('/user/:name', controller.userController.listTopics); // 展示用户自己发表的公开/私人文章(只有作者本人能看)
  router.post('/user/toggleViewType', controller.userController.toggleViewType); // 修改用户的展示模式(丰富or简洁)
  router.post('/user/hasUnreadTopic', controller.userController.hasUnreadTopic); // 用户是否有订阅未读文章

  router.post('/user/follow', controller.userController.follow); // 订阅
  router.post('/user/unfollow', controller.userController.unfollow); // 取消订阅
  router.get('/user/:name/attentions', controller.userController.listAttentions); // 我的订阅
  router.get('/user/:name/fans', controller.userController.listFans); // 我的粉丝
  router.get('/user/:name/more', controller.userController.index); // 用户个人主页

  // message controler
  router.get('/my/messages', userRequired, controller.messageController.index); // 用户个人的所有消息页

  // 新建文章界面
  router.get('/topic/create', userRequired, controller.topicController.create);

  router.get('/topic/:tid', controller.topicController.index); // 显示某个话题
  router.post('/topic/:tid/top', adminRequired, controller.topicController.top); // 将某话题置顶
  router.post('/topic/:tid/good', adminRequired, controller.topicController.good); // 将某话题加精
  router.get('/topic/:tid/edit', userRequired, controller.topicController.showEdit); // 编辑某话题
  router.post('/topic/:tid/lock', adminRequired, controller.topicController.lock); // 锁定主题，不能再回复

  router.post('/topic/:tid/delete', userRequired, controller.topicController.delete);

  // 保存新建的文章
  router.post('/topic/create', userRequired, createTopicLimit, controller.topicController.put);

  router.post('/topic/:tid/edit', userRequired, controller.topicController.update);

  router.post('/topic/:tid/incrementVisitCount', controller.topicController.incrementVisitCount); // 访问量+1

  router.post('/topic/assertTopicExist', userRequired, controller.topicController.assertTopicExist);


  // 收藏相关接口
  router.post('/collect/collect', userRequired, controller.collectController.collect); // 收藏文章
  router.post('/collect/de_collect', userRequired, controller.collectController.de_collect); // 取消收藏文章
  router.post('/collect/updateTagName', userRequired, controller.collectController.updateTagName); // 修改标签名
  router.post('/collect/removeTag', userRequired, controller.collectController.removeTag); // 删除标签
  router.get('/user/:name/collections', controller.collectController.listCollectedTopics); // 展示用户收藏的文章列表
  router.get('/collectionsScroll', controller.collectController.listCollectedTopicsScroll); // 展示用户收藏的文章列表(滚动分页)
  router.get('/collect/listTags', userRequired, controller.collectController.listTags); // 展示用户所有标签及标签包含文章的数量
  router.get('/collect/listTagsOnTopic', userRequired, controller.collectController.listTagsOnTopic); // 展示用户收藏此篇文章时分配的标签
  router.post('/collect/simpleCollect', userRequired, controller.collectController.simpleCollect); // 快速收藏


  // reply controller
  router.post('/:topic_id/reply', userRequired,
    // limit.peruserperday('create_reply', config.create_reply_per_day, { showJson: false }),
    controller.replyController.add); // 提交一级回复
  router.get('/reply/:reply_id/edit', userRequired, controller.replyController.showEdit); // 修改自己的评论页
  router.post('/reply/:reply_id/edit', userRequired, controller.replyController.update); // 修改某评论
  router.post('/reply/:reply_id/delete', userRequired, controller.replyController.delete); // 删除某评论
  router.post('/reply/:reply_id/up', userRequired, controller.replyController.up); // 为评论点赞
  router.post('/upload', userRequired, controller.topicController.upload); // 上传图片
  router.post('/upload4vditor', userRequired, controller.topicController.upload4Vditor); // 上传图片(vditor)
  // static page
  router.get('/robots.txt', controller.pageController.robots);


  // 搜索相关
  router.get('/search', controller.searchController.searchTopics);
  router.post('/searchBaidu', controller.searchController.pollingBaidu);
  router.post('/searchTsou', controller.searchController.pollingTsou);


  router.get('/history', controller.searchController.history);

  router.get('/cloud/login', controller.cloudController.render_login);
  router.get('/cloud/collect', controller.cloudController.render_collect);

  router.get('/customize', controller.customizeController.toCustomize);
  router.post('/customize', controller.customizeController.customize);
  router.get('/customize/delete/:id', controller.customizeController.delete);

  router.get('/alert', controller.alertController.toAlert);
  router.get('/alert/preview', userRequired, controller.alertController.previewAlerts);
  router.post('/alert/upsertAlert', userRequired, controller.alertController.upsertAlert);
  router.post('/alert/deleteAlert', userRequired, controller.alertController.deleteAlert);
  router.post('/alert/updateDeliveryHours', userRequired, controller.alertController.updateDeliveryHours);

};
